<%@page import="classes.Valuta"%>
<%@include file="template/configuration.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
            ${website} || 
            <c:choose>
                <c:when test="${not empty coursetoedit}">Edit</c:when>
                <c:otherwise>Add</c:otherwise>
            </c:choose>course
        </title>
        ${stylesheet}
        ${jquery}
        ${jsdropdown}
        ${jslist}
        ${autoFilter}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>Manage courses</h1>
                <h3>
                    <c:choose>
                        <c:when test="${not empty coursetoedit}">Edit</c:when>
                        <c:otherwise>Add</c:otherwise>
                    </c:choose>course
                </h3>
                <form action="../ManageCourse" method="POST">                                                   
                    <c:choose><c:when test="${not empty coursetoedit}"><input type="hidden" value="edit" name="formoption"/></c:when><c:otherwise><input type="hidden" value="add" name="formoption"/></c:otherwise></c:choose>
                    <input type="hidden" value="${coursetoedit.id}" name="idcourse"/>
                    <label for="name">Name</label> <input type="text" name="name" value="${coursetoedit.name}" id="name" required/>
                    <span class="error">${error.name}</span>
                    </br>
                    <label for="description">Description</label> <textarea name="description" id="description" required>${coursetoedit.description}</textarea>
                    </br>
                    <c:choose><c:when test="${sessionScope.userrole == 'Administrator'}">
                            <c:set var="price" value="${coursetoedit.price}" />
                            <label for="price">Price in euro</label> <input type="text" name="price" id="price" value="<c:choose><c:when test="${not empty coursetoedit.name}"><%=Valuta.calculateToCurrency("euro", (Double) Double.parseDouble(pageContext.getAttribute("price").toString()))%></c:when><c:otherwise>0,00</c:otherwise></c:choose>" id="price" required/> 
                            <span class="error">${error.price}</span>
                        </c:when><c:otherwise></c:otherwise>
                    </c:choose>
                    <c:choose><c:when test="${sessionScope.userrole == 'Administrator'}">
                            <input type="hidden" name="currency" value="euro">
                            </c:when><c:otherwise><input type="hidden" value="${coursetoedit.price}" name="price"/><input type="hidden" value="${coursetoedit.currency}" name="currency"/></c:otherwise></c:choose>
                    <c:choose>

                        <c:when test="${fn:length(categories)>0}">
                            <c:choose><c:when test="${sessionScope.userrole=='Administrator'}">
                                    <c:if test="${catid==null}">
                                        </br><label for="category">Category</label>
                                    </c:if>

                                    <c:choose>
                                        <c:when test="${catid==null}">
                                            <div class="relative">
                                                <select class="select" name="category" id="category">
                                                    <option>Choose category</option>
                                                    <c:forEach items="${categories}" var="categories">                                                                                                   
                                                        <option <c:if test="${coursetoedit.category.id == categories.id}">selected</c:if> value="${categories.id}">${categories.name}</option>                                        
                                                    </c:forEach>
                                                </select>
                                                <span class="pointer"><span>
                                                        </div> 
                                                    </c:when>
                                                    <c:otherwise>
                                                        <c:forEach items="${categories}" var="categories">                                                                                                   
                                                            <c:if test="${catid == categories.id}">
                                                                <input type="hidden" class="select" name="category" id="category" value="${categories.id}">
                                                            </c:if>                                        
                                                        </c:forEach>                                                    
                                                    </c:otherwise>
                                                </c:choose>                                
                                                <!-- clear //-->
                                                <div class="clear margin-bottom-5"></div>
                                                <h3>Select teacher(s)</h3>
                                                <label for="search1">Search</label> <input type="text" id="search1" value=""/>
                                                <br />
                                                <table style="width: auto;">
                                                    <tr>
                                                        <td>
                                                            <select class="optionbox" id="teacher" name="items_left" size="10" multiple="multiple" style="width:150px">                                                                
                                                                <c:forEach items="${employees}" var="teachers">
                                                                    <c:set var="valuet" value="false"/>
                                                                    <c:forEach items="${coursetoedit.teacher}" var="teach"> 
                                                                        <c:if test="${teach.id == teachers.id}">
                                                                            <c:set var="valuet" value="true"/>
                                                                        </c:if>                                                                
                                                                    </c:forEach>    
                                                                    <c:if test="${valuet != true }">
                                                                        <option value="${teachers.id}" name="${teachers.firstname}">${teachers.firstname} ${teachers.insertion} ${teachers.lastname}</option>
                                                                    </c:if>

                                                                </c:forEach>    
                                                            </select>                                                                                                                                                                                       
                                                        </td>                                                        
                                                        <c:if test="${sessionScope.userrole=='Administrator'}">
                                                            <td >              
                                                                <input type="button" value = "&DoubleRightArrow;" onClick="move_item(items_left, selectedteachers)" style="margin: 2px 10px"/><br>
                                                                <input type="button" value = "&DoubleLeftArrow;" onClick="move_item(selectedteachers,items_left)" style="margin: 2px 10px"/>                                                        
                                                            </td>
                                                            <td>
                                                                <select class="optionbox" id="selectX" name="selectedteachers" size="10" multiple="multiple" style="width:150px">
                                                                    <c:forEach items="${coursetoedit.teacher}" var="teacher">
                                                                        <option value="${teacher.id}" name="${teacher.id}" selected>${teacher.firstname} ${teacher.insertion} ${teacher.lastname}</option>
                                                                    </c:forEach>
                                                                </select>
                                                            </td>
                                                        </c:if>                                                                                                
                                                    </tr>
                                                </table>


                                            </c:when><c:otherwise><input type="hidden" name="category" value="${coursetoedit.category.id}"/></c:otherwise></c:choose>

                                                <!-- clear //-->
                                                <div class="clear margin-bottom-5"></div>
                                                <label>&nbsp;</label> <input type="submit" value="Save course" onclick="selectAll();"/>
                                                </form>
                                    </c:when>
                                    <c:otherwise>
                                        </br>
                                        You can not add a new course if there are no categories added!
                                    </c:otherwise>
                                </c:choose>
                                </form>
                                </div>
                                <%@include file="template/footer.jsp" %>
                                </div>
                                </body>
                                </html>
